package gui

/**
 * https://my.oschina.net/90design/blog/1604349
 * go get github.com/go-sql-driver/mysql
 */
import (
	"encoding/json"
	"github.com/lxn/walk"
	. "github.com/lxn/walk/declarative"
)

func GUIDemo() {
	var inTE, outTE *walk.TextEdit
	MainWindow{
		Title:   "格式化工具",
		MinSize: Size{600, 400},
		Layout:  VBox{},
		Children: []Widget{
			HSplitter{
				Children: []Widget{
					TextEdit{AssignTo: &inTE},
					TextEdit{AssignTo: &outTE, ReadOnly: true},
				},
			},
			PushButton{
				Text:    "格式化",
				MinSize: Size{150, 36},
				OnClicked: func() {
					var jsonStr interface{}
					err := json.Unmarshal([]byte(inTE.Text()), &jsonStr)
					jsonStr, _ = json.MarshalIndent(jsonStr, "\r\n", "\t")
					if err != nil {
						// 暂时未找到设置颜色值
						//outTE.SetTextColor(walk.Color(32))
						outTE.SetText("数据格式不正确")
					} else {
						outTE.SetText(string(jsonStr.([]uint8)))
					}
				},
			},
		},
	}.Run()
}


func GUIDemo2() {
	var inTE, outTE *walk.TextEdit
	var RadioTypeTab,RadioTypeSpace *walk.RadioButton

	MainWindow{
		Title:   "格式化工具",
		MinSize: Size{600, 400},
		Layout:  VBox{},
		Children: []Widget{
			HSplitter{
				MinSize: Size{600, 300},
				Children: []Widget{
					TextEdit{AssignTo: &inTE, HScroll:true, VScroll:true},
					TextEdit{AssignTo: &outTE, ReadOnly: true, HScroll:true, VScroll:true},
				},
			},
			Composite{
				MinSize: Size{600, 50},
				Layout:  Grid{Columns: 6, Spacing: 10},
				Children: []Widget{
					HSplitter{
						Children: []Widget{
							RadioButtonGroup{
								Buttons: []RadioButton{
									RadioButton{Name: "SplitType", Text: "Tab", Value: "Tab", RowSpan: 3, ToolTipText: "这是默认选项",AssignTo:&RadioTypeTab},
									RadioButton{Name: "SplitType", Text: "空格", Value: "space", ToolTipText: "4个空格",AssignTo:&RadioTypeSpace}}},
						},
					},
				},
			},
			PushButton{
				Text:    "格式化",
				MinSize: Size{150, 36},
				OnClicked: func() {
					typeStr := "    "
					checked := RadioTypeTab.Checked()
					if checked {
						typeStr = "\t"
					}

					var TextColor walk.Color
					if inTE.Text() == "" {
						TextColor = walk.RGB(byte(255), byte(48), byte(48))
						outTE.SetTextColor(TextColor)
						outTE.SetText("数据为空")
						return
					}
					var jsonStr interface{}
					err := json.Unmarshal([]byte(inTE.Text()), &jsonStr)
					jsonStr, _ = json.MarshalIndent(jsonStr, "\r\n", typeStr)
					if err != nil {
						TextColor = walk.RGB(byte(255), byte(48), byte(48))
						outTE.SetTextColor(TextColor)
						outTE.SetText("数据格式不正确")
						return
					}
					TextColor = walk.RGB(byte(34), byte(139), byte(34))
					//outTE.SetTextSelection(1, 10)
					outTE.SetTextColor(TextColor)
					outTE.SetText(string(jsonStr.([]uint8)))
				},
			},
		},
	}.Run()
}